import {defineConfig, loadEnv} from 'vite';
import {fileURLToPath, URL} from 'node:url';
import {visualizer} from "rollup-plugin-visualizer";
import react from '@vitejs/plugin-react';
import Icons from 'unplugin-icons/vite';
import AutoImport from 'unplugin-auto-import/vite';
import tailwindcss from "@tailwindcss/vite";


export default ({mode}: { mode: Parameters<typeof loadEnv>[0] }) => {
    const env: Record<string, string> = loadEnv(mode, process.cwd());

    return defineConfig({
        server: {
            host: '0.0.0.0',
            cors: true,
            port: 8080,
            proxy: {
                '/api': {
                    target: env.VITE_APP_LOCAL_URL,
                    changeOrigin: true,
                    ws: false,
                    rewrite: (path: string) => path.replace(/^\/api/, ""),
                }
            }
        },
        css: {
            preprocessorOptions: {
                less: {
                    javascriptEnabled: true
                }
            }
        },
        plugins: [
            react(),
            tailwindcss(),
            visualizer({
                open: false,  //注意这里要设置为true，否则无效
                filename: "stats.html", //分析图生成的文件名
                gzipSize: true, // 收集 gzip 大小并将其显示
                brotliSize: true, // 收集 brotli 大小并将其显示
            }),
            Icons({
                compiler: 'jsx',
                jsx: 'react'
            }),
            AutoImport({
                imports: ['react', 'react-router-dom', 'ahooks'],
                dts: 'auto-import.d.ts'
            })
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        build: {
            sourcemap: false,
            minify: 'terser',
            chunkSizeWarningLimit: 2000,
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true,
                    // 混淆变量名
                    properties: true,
                    // 混淆成简短的变量名
                    keep_fnames: true
                }
            },
            rollupOptions: {
                output: {
                    chunkFileNames: ({name}) => {
                        if (name === 'vendor') {
                            return `assets/js/[name]-[hash].js`; // 第三方库不添加时间戳
                        } else {
                            return `assets/js/[name]-[hash].js`; // 自定义文件名，使用时间戳保证唯一性
                        }
                    },
                    entryFileNames: ({name}) => {
                        if (name === 'vendor') {
                            return `assets/js/[name]-[hash].js`; // 第三方库不添加时间戳
                        } else {
                            return `assets/js/[name]-[hash].js`; // 自定义文件名，使用时间戳保证唯一性
                        }
                    },
                    assetFileNames: `assets/[ext]/[name]-[hash].[ext]` // 资源文件添加时间戳
                }
            }
        }
    })
}
